package com.ruoyi.core.entity;

import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 业务数据必须包含：id、projectId、statusCode、stateId、machineId字段
 *
 * @author wanghao 913351190@qq.com
 * @create 2023/12/5 14:05
 */
public class BaseMap extends LinkedHashMap<String, Object> {

    public BaseMap() {
    }

    public BaseMap(String key, Object value) {
        if (!StringUtils.isEmpty(key)) {
            this.put(key, value);
        }
    }

    public BaseMap(Map<String, Object> map) {
        if (map != null) {
            this.putAll(map);
        }
    }

    public Object getKey(Object key) {
        if (key == null) {
            return null;
        }
        if (super.containsKey(key)) {
            return key;
        } else {
            String s = this.keySet().stream().filter(x -> x.equalsIgnoreCase(key.toString())).findAny().orElse(null);
            if (StringUtils.isEmpty(s)) {
                s = this.keySet().stream().filter(x -> x.replace("_", "").equalsIgnoreCase(key.toString().replace("_", ""))).findAny().orElse(null);
            }
            if (!StringUtils.isEmpty(s)) {
                return s;
            }
        }
        return key;
    }

    @Override
    public Object get(Object key) {
        if (key == null) {
            return null;
        }
        return super.get(getKey(key));
    }

    @Override
    public Object remove(Object key) {
        return super.remove(getKey(key));
    }

    @Override
    public boolean containsKey(Object key) {
        if (key == null) {
            return false;
        }
        Object key1 = getKey(key);
        if (key.equals(key1)) {
            return super.containsKey(key);
        } else {
            return true;
        }
    }

    @Override
    public Object put(String key, Object value) {
        Object key1 = getKey(key);
        return super.put(key1 == null ? key : key1.toString(), value);
    }

    @Override
    public void putAll(Map<? extends String, ?> m) {
        for (String s : m.keySet()) {
            this.put(s, m.get(s));
        }
    }

    public Object getObject(String key) {
        return get(key);
    }

    public String getString(String key) {
        Object v = getObject(key);
        return v == null ? null : v.toString();
    }

    public Integer getInteger(String key) {
        Object v = getObject(key);
        return v == null ? null : Integer.parseInt(v.toString());
    }

    public Integer getInteger(String key, int defaultValue) {
        Object v = getObject(key);
        return v == null ? defaultValue : Integer.parseInt(v.toString());
    }

    public Boolean getBoolean(String key) {
        Object v = getObject(key);
        return v != null && (
                "true".equalsIgnoreCase(v.toString()) ||
                        "1".equals(v.toString()) ||
                        "Y".equalsIgnoreCase(v.toString()) ||
                        "Yes".equalsIgnoreCase(v.toString())
        );
    }

    public Boolean getBoolean(String key, boolean defaultValue) {
        Object v = getObject(key);
        return v == null ? defaultValue : (
                "true".equalsIgnoreCase(v.toString()) ||
                        "1".equals(v.toString()) ||
                        "Y".equalsIgnoreCase(v.toString()) ||
                        "Yes".equalsIgnoreCase(v.toString())
        );
    }

    public boolean isEmpty(String key) {
        return StringUtils.isEmpty(getString(key));
    }

}
